package com.qishanor.framework.satoken;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qishanor.framework.constant.CacheConstant;
import com.qishanor.system.mapper.SysMenuMapper;
import com.qishanor.system.mapper.SysRoleMapper;
import com.qishanor.system.mapper.SysRoleMenuMapper;
import com.qishanor.system.mapper.SysUserRoleMapper;
import com.qishanor.system.model.SysMenu;
import com.qishanor.system.model.SysRole;
import com.qishanor.system.model.SysRoleMenu;
import com.qishanor.system.model.SysUserRole;
import com.qishanor.system.service.SysMenuService;
import com.qishanor.system.service.SysRoleMenuService;
import com.qishanor.system.service.SysRoleService;
import com.qishanor.system.service.SysUserRoleService;
import com.qishanor.system.service.impl.SysMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: Sa-Token自定义权限验证接口扩展
 * @author: 周振林
 * @date: 2022-04-09
 * @Copyright: 博客：http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@Component
public class StpInterfaceImpl implements StpInterface {


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;



    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        //通过用户ID 获取到角色ID
        List<Integer> roleIds= sysUserRoleMapper.selectObjs(Wrappers.lambdaQuery(SysUserRole.class)
                .select(SysUserRole::getRoleId).eq(SysUserRole::getUserId,loginId))
                .stream().map(u->(Integer)u).collect(Collectors.toList());

        //通过角色ID 获取到菜单ID
        List<Integer> menuIds=sysRoleMenuMapper.selectObjs(Wrappers.lambdaQuery(SysRoleMenu.class)
                .select(SysRoleMenu::getMenuId)
                .in(SysRoleMenu::getRoleId,roleIds))
                .stream().map(u->(Integer)u).collect(Collectors.toList());

        //通过菜单ID 查询所有权限
        List<String> list=sysMenuMapper.selectBatchIds(menuIds)
                .stream().map(u-> u.getPermission()).filter(u->StrUtil.isNotBlank(u)).collect(Collectors.toList());

        return list;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {

        //通过用户ID 查询角色ID
        List<Integer> roleIds= sysUserRoleMapper.selectObjs(Wrappers.lambdaQuery(SysUserRole.class)
                .select(SysUserRole::getRoleId).eq(SysUserRole::getUserId,loginId))
                .stream().map(u->(Integer)u).collect(Collectors.toList());

        //通过角色ID 查询角色名称
        List<String> list=sysRoleMapper.selectBatchIds(roleIds).stream().map(u->u.getCode()).collect(Collectors.toList());

        StpUtil.getSession().set(CacheConstant.ROLE_DETAIL,roleIds);
        return list;
    }
}
